package jdbctest;

import homework.jdbc.dao.JDriverUtils;

import java.sql.*;

/**
 * Created by Administrator on 2017/9/11.
 */
public class ConnectionTest
{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//        //1.加载驱动
//        Class.forName("com.mysql.jdbc.Driver");
//        //2.连接数据库（对象）-->三个参数（url/用户名，密码）
//
//        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/mysql","root","123456");
       Connection conn = JDriverUtils.getConnection();
        if (conn!=null){
            System.out.println("ok");
        }else{
            System.out.println("error");
        }

        //操作数据 连接后不能直接操作
        Statement statement =conn.createStatement();
        //查询
        String sql="select*from student";

        //mysql 数据放入 ResultSet集合中
        ResultSet set=statement.executeQuery(sql);
        while (set.next()){
            //直接读取
//            System.out.println(set.getString(1)+" "+
//                               set.getString(2)+" "+
//                               set.getString(3));
            //使用对象读取
 /*           User user=new User();
            user.setSno(set.getInt("sno"));
            user.setSname(set.getString(2));
            user.setSage(set.getInt("sage"));
            System.out.println(user);*/
        }
    }
}
